//
//  ZipArchive+ZipDir.m
//  PetGuGu
//
//  Created by _G.R.M. on 2023/8/17.
//

#import "ZipArchive+ZipDir.h"

@implementation ZipArchive (ZipDir)

+ (BOOL)doZipAtPath:(NSString *)sourcePath to:(NSString *)destZipFile {
    return [[self class] doZipAtPath:sourcePath to:destZipFile pwd:@""];
}

+ (BOOL)doZipAtPath:(NSString *)sourcePath to:(NSString *)destZipFile pwd:(NSString *)pwd {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    ZipArchive *zipArchive = [ZipArchive new];
    if (pwd.length) {
        [zipArchive CreateZipFile2:destZipFile Password:pwd];
    } else {
        [zipArchive CreateZipFile2:destZipFile];
    }
    NSArray *subPaths = [fileManager subpathsAtPath:sourcePath];
    for(NSString *subPath in subPaths){
        NSString *fullPath = [sourcePath stringByAppendingPathComponent:subPath];
        BOOL isDir;
        if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir) {
            [zipArchive addFileToZip:fullPath newname:subPath];
        }
    }
    return [zipArchive CloseZipFile2];
}

@end
